package com.jh.storageview;

import static com.jh.storageview.StorageFragment.STORAGE_APP;
import static com.jh.storageview.StorageFragment.STORAGE_DOC;
import static com.jh.storageview.StorageFragment.STORAGE_IMG;
import static com.jh.storageview.StorageFragment.STORAGE_PKG;
import static com.jh.storageview.StorageFragment.STORAGE_SOUND;
import static com.jh.storageview.StorageFragment.STORAGE_SYS_D;
import static com.jh.storageview.StorageFragment.STORAGE_SYS_G;
import static com.jh.storageview.StorageFragment.STORAGE_VIDEO;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.PointF;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class StorageViewLine extends View {

    public static final String TAG = "StorageViewLine";

    Context mContext;
    float rootWidth, rootHeight;
    PointF mPointF;
    Paint mPaint;

    int[] dys;
    int index = 0;
    boolean[] arrays;
    boolean isClear;
    int[] colors = new int[]{
            getResources().getColor(R.color.sys_guR, null),
            getResources().getColor(R.color.sys_dataR, null),
            getResources().getColor(R.color.docR, null),
            getResources().getColor(R.color.pkgR, null),
            getResources().getColor(R.color.videoR, null),
            getResources().getColor(R.color.soundR, null),
            getResources().getColor(R.color.imgR, null),
            getResources().getColor(R.color.appR, null),
    };


    public StorageViewLine(Context context) {
        super(context);
        mContext = context;
        init();
    }

    public StorageViewLine(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        init();
    }

    public StorageViewLine(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        init();
    }

    public StorageViewLine(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        mContext = context;
        init();
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        rootWidth = getWidth();
        rootHeight = getHeight();
    }

    private void init(){
        mPointF = new PointF();
        mPaint = new Paint();
        mPaint.setColor(Color.RED);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(5);
    }

    @Override
    protected void onDraw(@NonNull Canvas canvas) {
        super.onDraw(canvas);
        if (dys != null && dys.length > 0){
            if (mPointF.y != 0){
                mPaint.setColor(colors[index]);
                canvas.drawLine(0, mPointF.y, rootWidth - 50, dys[index], mPaint);
                canvas.drawLine(rootWidth - 50, dys[index], rootWidth, dys[index], mPaint);
            }
        }
    }

    public void setItemArrays(boolean[] arrays){
        this.arrays = arrays;
        for (int i = 0; i < arrays.length; i++) {
            if (arrays[i]){
                index = i;
                break;
            }
        }
        Log.d("jianghao", "setItemArrays: index = " + index);
    }
    public void setPointF(PointF pointF){
        mPointF.set(pointF);
        isClear = false;
        invalidate();
    }

    public void setPointRF(int[] dy){
        dys = dy;
        for (int i : dys) {
            Log.d("jianghao", "setItemArrays: i = " + i);
        }
        invalidate();
    }

    public void clearLine(){
        isClear = true;
        invalidate();
    }

}
